package com.pm.controller.log;

import java.util.Date;
import java.util.List;

import com.pm.common.annotation.CurrentUser;
import com.pm.entity.po.security.User;
import com.pm.mapper.security.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.dept.DepartmentScore;
import com.pm.entity.po.log.DepartmentScoreCheckLog;
import com.pm.service.dept.DepartmentScoreService;
import com.pm.service.log.DepartmentScoreCheckLogService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.xml.crypto.Data;

@Api(tags = "打分审核记录表")
@UserLoginToken
@RestController
@RequestMapping("/departmentScoreCheckLog")
public class DepartmentScoreCheckLogController {
    @Autowired
    DepartmentScoreCheckLogService departmentScoreCheckLogService;
    @Autowired
    UserMapper userMapper;
    @Autowired
    private DepartmentScoreService departmentScoreService;

    @ApiOperation("查询全部打分审核记录表接口")
    @PostMapping("/getList")
    @ResponseBody
    @MyLog(value = "查询全部打分审核记录")
    public Response<List<DepartmentScoreCheckLog>> getList(DepartmentScoreCheckLog departmentScoreCheckLog, @CurrentUser
            User user) {
        Integer id = user.getUserId();
        Integer taskId = userMapper.getTaskId(id);
        departmentScoreCheckLog.setTaskId(taskId);
        List<DepartmentScoreCheckLog> result = departmentScoreCheckLogService.getList(departmentScoreCheckLog);
        Response<List<DepartmentScoreCheckLog>> res = new Response<List<DepartmentScoreCheckLog>>(
                ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("修改打分审核记录读状态")
    @PostMapping("/updateReda")
    @ResponseBody
    @MyLog(value = "修改打分审核记录读状态")
    public Response<Integer> updateReda(DepartmentScoreCheckLog departmentScoreCheckLog) {
        int res = departmentScoreCheckLogService.update(departmentScoreCheckLog);
        if (res > 0) {
            return new Response<>(ExpCodeState.update_data_success, res);
        } else {
            return new Response<>(ExpCodeState.update_data_failed);
        }
    }

    @ApiOperation("更新打分审核记录")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "更新打分审核记录")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(DepartmentScoreCheckLog departmentScoreCheckLog, @CurrentUser
            User user) {
        DepartmentScore s = new DepartmentScore();
        // 修改审核状态 2 通过
        if (departmentScoreCheckLog.getStatus() == 1) {
            // 审核人
            departmentScoreCheckLog.setAuditor(user.getUserId());
            departmentScoreCheckLog.setUpdateTime(new Date());
            departmentScoreCheckLog.setStatus(1);
            // 添加记录
            int res = departmentScoreCheckLogService.update1(departmentScoreCheckLog);
            s.setIds(departmentScoreCheckLog.getIds());
            s.setStatus(2);
            departmentScoreService.update1(s);
            // 审核人
            if (res > 0) {
                return new Response<>(ExpCodeState.update_data_success, res);
            } else {
                return new Response<>(ExpCodeState.update_data_failed);
            }
        }
        // -1 驳回驳回
        if (departmentScoreCheckLog.getStatus() == -1) {
            // 添加驳回记录
            departmentScoreCheckLog.setAddTime(new Date());
            departmentScoreCheckLog.setAuditor(user.getUserId());
            departmentScoreCheckLog.setRead(0);
            departmentScoreCheckLog.setTaskId(departmentScoreCheckLog.getTaskId());
            departmentScoreCheckLog.setQuarter(departmentScoreCheckLog.getQuarter());
            departmentScoreCheckLog.setStatus(-1);
            departmentScoreCheckLog.setMessage(departmentScoreCheckLog.getMessage());
            int res = departmentScoreCheckLogService.addReject(departmentScoreCheckLog);
            s.setIds(departmentScoreCheckLog.getIds());
            s.setStatus(-1);
            departmentScoreService.update2(s);
            if (res > 0) {
                return new Response<>(ExpCodeState.update_data_success, res);
            } else {
                return new Response<>(ExpCodeState.update_data_failed);
            }
        }
        return new Response<>(ExpCodeState.update_data_failed);
    }
}
